package de.koller.million.card.engine.item;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

public class Item {

	public final long id;
	
	protected ItemQuality quality = ItemQuality.Poor;
	protected ItemSlot slot = null;
	protected int ilvl = -1;
	protected final Map<ItemProperty, Double> properties = new HashMap<>();
	
	protected double damage = 0;

	public Item( long id ) {
		this.id = id;
	}

//	public void saveItem() {
//		if( id == -1 )
//			throw new OceanException( "Item has id -1, should only use for views and not saved!" );
//		
//		MOut o = new MOut( "./cardData/items/i_" + id + ".ci" );
//		o.writeLong( id );
//		o.writeString( quality.name() );
//		o.writeString( slot.name() );
//		o.writeInt( ilvl );
//		o.writeDouble( damage );
//		o.writeInt( properties.size() );
//		for( ItemProperty i : properties.keySet() ) {
//			o.writeString( i.name() );
//			o.writeDouble( (double)properties.get( i ) );
//		}
//		o.close();
//	}
	
//	public static Item loadItem( int id ) {
//		Item i = new Item( id );
//		MIn o = new MIn( "./cardData/items/i_" + id + ".ci" );
//		o.readLong( 1 );
//		i.quality = ItemQuality.valueOf( o.readString( 1 )[0] );
//		i.slot = ItemSlot.valueOf( o.readString( 1 )[0] );
//		i.ilvl = o.readInt( 1 )[0];
//		i.damage = o.readDouble( 1 )[0];
//		int j = o.readInt( 1 )[0];
//		for( int k = 0; k < j; k++ ) {
//			ItemProperty p = ItemProperty.valueOf( o.readString( 1 )[0] );
//			double v = o.readDouble( 1 )[0];
//			i.properties.put( p, v );
//		}
//		o.close();
//		return i;
//	}
	
//	public void destroyItem() {
//		File f = new File( "./cardData/items/i_" + id + ".ci" );
//		f.delete();
//	}
	
	/** bereits berechnet! */
	public double getDamage() {
		return calcValue( damage );
	}

	public int getIlvl() {
		return ilvl;
	}

	public Color getQualityColor() {
		return new Color( quality.color );
	}

	public ItemSlot getSlot() {
		return slot;
	}
	
	public double calcValue( double v ) {
		double r = v * ( (double)ilvl + quality.factor ) * slot.factor;
		if( r < 0 ) r = 0;
		return r;
	}

	public double getPropertyValue( ItemProperty p ) {
		double v = properties.get( p );
		return calcValue( v );
	}
	
	public Map<ItemProperty, Double> getProperties() {
		return properties;
	}

	public ItemQuality getQuality() {
		return quality;
	}
	
	/** add html tag! */
	public String toPrintString() {
		String s = "";
		s += "<font color=\"white\" style=\"BACKGROUND-COLOR: black \">";
		s += "'" + slot.displayName + " [";
		s += "<b><font color=\"" + Integer.toHexString( quality.color ) + "\">" + Math.round( getDamage() ) + "</font></b>";
		s += "] ";
		s += "<i>" + ilvl + " </i>";
		s += "</font>";
		s += "";
		return s;
	}
	
}
